/*
	普通商品列表
		商品列表
		商品详情查看
		商品添加

*/
//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');


//dom 加载完成
$(function(){
	layui.use(['layer', 'table', 'form', 'laydate'], function(){
		var layer = layui.layer,
			table = layui.table,
			form = layui.form,
			laydate = layui.laydate;

		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken : adminLoginedInfo.aToken
		};

		//获取商品列表	
		function getProducts(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend( {pType: 'normal'}, ajaxData);

			$.ajax({
				url: BASE_SERVICEURL + URlS.product,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						//console.log(responseData);
						var tableData = createTableData(responseData);
						//console.log(tableData)
						createTable(tableData);
						layer.close(loadIndex);
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						//跳转500
						location.href = './500.html';
					}});
				}
			});
		};

		getProducts(ajaxData)

		//构建表格数据
		function createTableData(responseData){
			var data = responseData.data;
			var table_data_arr = [];
			$('#dataSize').text(data.length);
			for(var i = 0; i < data.length; i++){
				var _item = {
					pId: data[i].pId,
					thumbnail: data[i].pImg[0],  //此处应当使用模板
					brand: data[i].pBrand,
					series: data[i].pSeries,
					name: data[i].pName['cn'],
					stock: data[i].pStock,
					price: 'R' + data[i].pPrice['cn'] + ';' + 'T' + data[i].pPrice['en'] + ';' + 'U' + data[i].pPrice['tw'],
					size: forEachObj(data[i].pSize),
					createTime: formatTime(data[i].formatTime),
					admin: data[i].pCreateAdmin,
					psn: spliceArr(data[i].accessPromotional)
				}
				table_data_arr.push(_item);
			}
			return table_data_arr;
		}
		//构建table表格
		function createTable(tableData){

			table.render({
				elem: '#tableContent',
				limit: 10, //分页
				height: 620,
				page: true,
				cols:[[ //表头
					{field: 'pId', title: '商品ID', sort: true, fixed: 'left', width:120},
					{field: 'thumbnail', title: '缩略图', width:110, templet: function(d){  //自定义模板
						//console.log(d)
						return '<img src="' + d.thumbnail + '?imageView2/1/w/90/h/90/q/75|imageslim" alt="商品缩略图">'
					}},
					{field: 'brand', title: '品牌',width:110, sort: true},
					{field: 'series', title: '系列', sort: true, width:60} ,
					{field: 'name', title: '名称', width:110},
					{field: 'stock', title: '库存', width:80},
					{field: 'price', title: '单价(R;T;U)', sort: true},
					{field: 'size', title: '支持尺寸',width:110},
					{field: 'createTime', title: '加入时间', sort: true},
					{field: 'admin', title: '创建管理员',width:80},
					{field: 'psn', title: '应用活动', sort: true},
					{field: 'option', title: '操作',width:120, toolbar:'#toolbar'}
				]],
				data:tableData
			});
		}

		//高级检索
		form.on('submit(search)', function(data){
			//console.log(data);
			var _ajaxData = $.extend({}, ajaxData, data.field);
			//console.log(_ajaxData);
			getProducts(_ajaxData);
			return false;
		})

		//toolbar 监听 详情查看
		table.on('tool(nproduct-list-table)', function(obj){
			var data = obj.data;   //本行数据
			var layEvent = obj.event; 
			var tr = obj.tr;  //当前行
			//console.log(data)
			if(layEvent == 'seeDetatil'){  //查看详情
				x_admin_show('查看详情', 'normal-product-detail.html?pId='+data.pId+'', 1200, 800);
			};
		});


	});
})


